
#include "MSP430x54x.h"

#include "port.h"

#include "task.h"
#include "tick.h"

#include "chip.h"

#include "semaphore.h"

#include "isr.h"

#include "os.h"

#include "bsp.h"

#include "timer.h"

OS_TCB task1Tcb;
OS_TCB task2Tcb;
OS_TCB task3Tcb;
OS_TCB task4Tcb;
OS_TCB task5Tcb;


struct TIMER timer1;

struct TIMER timer2;

struct SEMAPHORE sem1;



void timer1Fun(void)
{
  _NOP();
}


void timer2Fun(void)
{
  _NOP();
}


void task1(void)
{
  setupTimer(&timer1,timer1Fun,PERIOD_TIMER,5000);
  
  startTimer(&timer1);
  while(1)
  {
    delayTick(3);
  }
}

void task2(void)
{
  setupTimer(&timer2,timer2Fun,PERIOD_TIMER,50);
  
  startTimer(&timer2);
  initSema(&sem1,0);
  while(1)
  {
    downSema(&sem1);
    
    P1OUT ^= BIT1;
  }
}

void task3(void)
{
  while(1)
  {
    delayTick(150);
  }
}

void task4(void)
{
  while(1)
  {
    delayTick(60);
  }
}

void task5(void)
{
  while(1)
  {
    P1OUT ^= BIT0;
    delayTick(5);
  }
}

int main( void )
{
 
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;

  initBsp();
  
  osInit();
  
  createTask(&task1Tcb,task1,2,256);
  createTask(&task2Tcb,task2,3,256);
  createTask(&task3Tcb,task3,4,256);
  createTask(&task4Tcb,task4,5,256);
  createTask(&task5Tcb,task5,6,256);
  
  osStart();
}

void port1Isr(UINT16 arg)
{
  upSema(&sem1);
}


#pragma vector=PORT1_VECTOR

__interrupt void portInt(void)
{
   UINT16 arg = 0;
   arg = P1IFG;
   P1IFG = 0;
   runIsr(port1Isr,arg);
}
